/**
 * 
 */
package com.hirstlogics.boxarrow.xml;

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import com.hirstlogics.boxarrow.box.Box;
import com.hirstlogics.boxarrow.model.Description;
import com.hirstlogics.boxarrow.model.IdManager;
import com.hirstlogics.boxarrow.model.Model;

/**
 * @author steve
 *
 */
public class IO {

	public static Box readBox(XMLStreamReader read) {
		Box box = null;
		if (Utility.openTag(read,Persist.BOX_TAG)) {
			String id = read.getAttributeValue(null,"id");
			String sort = read.getAttributeValue(null,"sort");
			try {
				Class boxClass = Class.forName(sort);
				box = (Box) boxClass.newInstance();
				box.setId(Integer.valueOf(id));
				box.readContents(read);
				read.next();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		else {
			// We get here when reading to end of boxes
			}
	
		// instantiate sort and use it to read contents
		// read end
		IdManager.getInstance().readThing(box);
		return box;
	}

	/**
	 * @param read
	 * @param result
	 * @return
	 * @throws XMLStreamException
	 */
	public static Description readDiv(XMLStreamReader read) throws XMLStreamException {
		Description divDesc = new Description("Div");
		if (read.getEventType()==XMLStreamConstants.START_ELEMENT &&
		    read.getLocalName().equals("div")) {
		    	read.next();
		    	if (read.getEventType()==XMLStreamConstants.CHARACTERS) {
		    		divDesc.setDesc(read.getText());
		    		read.next();
		    	}
		    	while(read.getEventType()!=XMLStreamConstants.END_ELEMENT) {
		    		divDesc.addDesc(readDiv(read));
		    	}
		    	Utility.closeTag(read);
		    }
		
		return divDesc;
	}

	static public Description readDescription(XMLStreamReader read) throws XMLStreamException {
		Description result = new Description("No Name");
		if (Utility.openTag(read,Persist.DESCRIPTION)) {
		    	result.clear();
		    	read.nextTag();
		    }
		result = readDiv(read);
		read.nextTag(); // Consume close tag for Description
		return result;
	}

	static public Model readModel(XMLStreamReader read) throws XMLStreamException {
		Model result = new Model("No Name");
		if (Utility.openTag(read, Persist.MODEL_TAG)) {
			    	String newName = read.getAttributeValue(null, "name");
			    	result.setName(newName);
			    	read.next();
			    	result.setDescription(readDescription(read));
					if (Utility.openTag(read, Persist.BOXES_TAG)) {
						    read.next();
			    	        Box newBox = readBox(read);
			    	        while (newBox!=null) {
			    	        	result.addBox(newBox);
			    	        	newBox = readBox(read);
			    	        }
					}
			    }
		return result;
	}

}
